package com.dragon.app.config.quartz;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragon.logical.service.avttendance.IAttendanceService;
import com.dragon.repository.entity.ArrangePersonnel;
import com.dragon.repository.entity.Attendance;
import com.dragon.repository.entity.AttendanceArrange;
import com.dragon.repository.entity.AttendanceChild;
import com.dragon.repository.mapper.ArrangePersonnelMapper;
import com.dragon.repository.mapper.AttendanceArrangeMapper;
import com.dragon.repository.mapper.AttendanceChildMapper;
import com.dragon.repository.mapper.AttendanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

//@Component // 把此类托管给 Spring，不能省略
public class TaskUtils {

//    @Autowired
//    ArrangePersonnelMapper personnelMapper;
//    @Autowired
//    AttendanceArrangeMapper arrangeMapper;
//    @Autowired
//    AttendanceChildMapper childMapper;
//    @Autowired
//    AttendanceMapper mapper;
//    @Autowired
//    IAttendanceService service;
//
//    // 添加考勤定时添加任务
//    @Scheduled(cron = "0 0 6  * * ?") // cron表达式：每天六点执行一次
//    public void doTask(){
//        //获取今天周几 1周天 2周一 3周二 4周三 5周四 6周五 7周六
//        Calendar instance = Calendar.getInstance();
//        int i = instance.get(Calendar.DAY_OF_WEEK);
//        //修改当前时间格式 yyyy-MM-dd
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        Date date = new Date();
//        String dd = sdf.format(date);
//
//        List<ArrangePersonnel> personnelList = personnelMapper.selectList(null);
//        personnelList.forEach(personnel -> {
//            AttendanceArrange arrange = arrangeMapper.selectById(personnel.getArrangeId());
//            //获取人员配置所有的人的id
//            List<Integer> adminIds = Stream.of(personnel.getAdminId().split(",")).map(Integer::valueOf).collect(Collectors.toList());
//            //获取当前用户需要上班的日期 判断是否需要打卡
//            List<Integer> dates  = Stream.of(arrange.getRecurry().split(",")).map(Integer::parseInt).collect(Collectors.toList());
//            if (dates.contains(i)==true){
//                adminIds.forEach(id ->{
//                    QueryWrapper<Attendance> wrapper = new QueryWrapper<>();
//                    Attendance attendanceOne = mapper.selectOne(wrapper.lambda().eq(Attendance::getAdminId, id)
//                            .apply(" date_format(create_time,'%Y-%mm-%dd') = date_format('" + dd + "','%Y-%mm-%dd')"));
//                    if (attendanceOne == null){
//                        Attendance attendance = new Attendance();
//                        attendance.setAdminId(id);
//                        attendance.setCreateTime(DateUtil.date());
//                        mapper.insert(attendance);
//
//                        AttendanceChild child = childMapper.selectOne(new QueryWrapper<AttendanceChild>().lambda()
//                                .eq(AttendanceChild::getAttendanceId, attendance.getId()));
//                        if (child == null){
//                            AttendanceChild attendanceChild = new AttendanceChild();
//                            attendanceChild.setAttendanceId(attendance.getId());
//                            childMapper.insert(attendanceChild);
//                        }
//                    }
//                });
//            }
//        });
//    }
//
//
//    //添加更新状态定时任务
//    @Scheduled(cron = "0 30 20 * * ? ")
//    public void changeTask(){
//        //修改当前时间格式 yyyy-MM-dd
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        Date date = new Date();
//        String dd = sdf.format(date);
//
//        List<ArrangePersonnel> personnelList = personnelMapper.selectList(null);
//        personnelList.forEach(personnel -> {
//            //获取人员配置所有的人的id
//            List<Integer> adminIds = Stream.of(personnel.getAdminId().split(",")).map(Integer::valueOf).collect(Collectors.toList());
//            for (Integer adminId : adminIds) {
//                Attendance attendance = mapper.selectOne(new QueryWrapper<Attendance>()
//                        .lambda().eq(Attendance::getAdminId, adminId)
//                        .apply(" date_format(create_time,'%Y-%mm-%dd') = date_format('" + dd + "','%Y-%mm-%dd')"));
//                AttendanceChild child = childMapper.selectOne(new QueryWrapper<AttendanceChild>()
//                        .lambda().eq(AttendanceChild::getAttendanceId, attendance.getId()));
//                if (child != null){
//                    service.updateType(attendance.getId());
//                }
//            }
//
//        });
//    }
//
//    @Bean
//    public TaskScheduler taskScheduler() {
//        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
//        taskScheduler.setPoolSize(2);//我这里设置的线程数是2,可以根据需求调整
//        return taskScheduler;
//    }


}